import {effects} from 'dva-no-router/saga'
import {message} from 'antd'
import {GET_SHOPCART, GET_SHOPCART_SUMMARY, POST_SHOPCART_ITEM, PUT_SHOPCART_ITEM} from '../api/shopcart'
import {setState, setStateReducer} from "../util/uitls";

const Cookie = require('js-cookie')
import Filter from 'lodash.filter'

export default {
    namespace: 'shopcart',
    state: {
        shopcartId: 0,
        totalQuantity: 0,
        addressId: 0,
        shopcartItems: [],
        unselectedItems: []
    },
    reducers: {
        setModel(state, {payload: {model}}) {
            return {
                ...model
            }
        },
        setState: setStateReducer,
        setUnselectedItems(state, {payload: {toggleItem}}) {
            let unselectedItems = [...state.unselectedItems];
            if (unselectedItems.find(item => item.id == toggleItem.id))
                unselectedItems = unselectedItems.filter(item => item.id != toggleItem.id);
            else
                unselectedItems.push(toggleItem)
            return {
                ...state,
                unselectedItems
            }
        },
        setLoading(state, {payload: {loading}}) {
            return {
                ...state,
                loading
            }
        },
    },
    effects: {
        * flowSelectAddress({payload}, {call, put}) {
            yield setState({addressId: payload})('shopcart')
        },
        * flowAddItem({payload: {shopcartId, designItemId, quantity, skuId, description}}, {call, put, select}) {
            let model = yield select(state => state.shopcart)
            const data = yield call(POST_SHOPCART_ITEM, shopcartId, designItemId, quantity, skuId, description)
            if (data.shopcartId) {
                Cookie.set("shopcartId", data.shopcartId, {expires: 5})
            }
            if (model.shopcartItems.find(item => item.id == data.id)) {
                model.shopcartItems = model.shopcartItems.map(item => item.id == data.id ? {
                    ...item,
                    quantity: data.quantity
                } : item)
            }
            else {
                /* model.shopcartItems =*/
                model.shopcartItems.push(data)
            }
            yield put({
                type: 'setModel',
                payload: {model}
            })
            message.success("加入购物车成功")
            /*主动计算购物车数量*/
            yield put({type: "flowGetSummaryThenItems", payload: {shopcartId: model.shopcartId}})
        },
        * flowUpdateItem({payload: {shopcartItemId, quantity}}, {call, put, select}) {
            let model = yield select(state => state.shopcart)
            const data = yield call(PUT_SHOPCART_ITEM, shopcartItemId, quantity)
            model.shopcartItems = model.shopcartItems.map(item => item.id == data.id ? {
                ...item,
                quantity: data.quantity
            } : item)
            model.shopcartItems = Filter(model.shopcartItems, item => {
                return item.quantity != 0
            });
            model.totalQuantity = model.shopcartItems.length;
            if (quantity == 0)
                model.unselectedItems = model.unselectedItems.filter(item => item.id != shopcartItemId);

            yield put({
                type: 'setModel',
                payload: {model}
            })
        },
        * flowGet(payload, {call, put, select}) {
            let model = yield select(state => state.shopcart)
            const data = yield call(GET_SHOPCART, model.shopcartId)
            model.shopcartId = data.id
            model.shopcartItems = data.shopcartItems
            model.totalQuantity = data.shopcartItems.map(item => item.quantity).reduce((all, val) => all + val)

            yield put({
                type: 'setModel',
                payload: {model}
            })
        },
        * flowGetSummary({payload}, {call, put, select}) {
            if (payload) {
                let model = yield select(state => state.shopcart)
                let data = yield call(GET_SHOPCART_SUMMARY, payload.shopcartId)
                model.shopcartId = data.id;
                model.shopcartItems = [];
                model.totalQuantity = data.itemCount;
                yield put({
                    type: 'setModel',
                    payload: {model}
                })
            }
        },
        * flowGetSummaryThenItems({payload: {shopcartId}}, {call, put, select}) {
            let model = yield select(state => state.shopcart)
            if (!model.shopcartId) {
                let data = yield call(GET_SHOPCART_SUMMARY, shopcartId)
                if (data) {
                    model.shopcartId = data.id;
                    model.shopcartItems = [];
                    model.totalQuantity = data.itemCount;
                    data = yield call(GET_SHOPCART, model.shopcartId)
                    if (data.shopcartItems)
                        model.shopcartItems = data.shopcartItems
                    yield put({
                        type: 'setModel',
                        payload: {model}
                    })
                }
            }
        },
        //
        // *flowInit(action, {call, put, select})
        // {
        //     let model = yield select(state => state.salesOrderList)
        //     yield put({
        //         type: 'setLoading',
        //         payload: {loading:true}
        //     })
        //     let result = yield call(fetchSalesOrderStatuses)
        //     model.salesOrderStatuses = result.data.result
        //     result = yield call(fetchSalesOrders, model)
        //     yield call(processFetchResult, model, result, put)
        // }

        // *flowAddClick(action, {put}){
        //     yield put(routerRedux.push('/sales-order/item'));
        // },
    }
}

// function *processFetchResult(model, result, put) {
//     model.salesOrders = result.data.result
//     model.paging = result.data.paging
//     model.loading = false
//     yield put({
//         type: 'setModel',
//         payload: {model}
//     })
// }